package pluginConvertisseur;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.HashMap;
import java.util.Set;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import compatibilite.AExcelMiniVue;

/**************
 * 
 * @author Axel
 *
 */
public class VueConvertisseur extends AExcelMiniVue implements ActionListener, TableModelListener, ItemListener 
{
	JTable tableDestination, tableSource;
	JComboBox<Object> comboSource, comboResult, comboGeneral;
	JButton btnIntervertir, btnConvertir;
	JPanel paneGeneral;
	HashMap<String,HashMap<String,HashMap<String,Float>>> listeGenerale;
	HashMap<String,HashMap<String,Float>> listeSource;
	HashMap<String,Float> listeDestination;
	TableModel tm;

	public void majListeResultats(TableModel tmc) {
		tableDestination.setModel(tmc);
		tableDestination.getColumnModel().getColumn(0).setHeaderValue("Résultat :");
	}
	public void majListeSource(TableModel tmc){
		int i;
		viderTable(tableSource);
		//bug sur la longueur de la liste, limitation a 10 en attendant 
		//tmc.getRowCount()
		for(i=0;i<10;i++){
			try{
				tm.setValueAt(Float.parseFloat((String) tmc.getValueAt(i, 0)), i, 0);
			}
			catch(NullPointerException caseNulle){}
			catch(NumberFormatException nonConvertible){
				JOptionPane.showMessageDialog(null, "Impossible de convertir : "+
						(String) tmc.getValueAt(i, 0), "Conversion impossible", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	public VueConvertisseur(){
	}
	@SuppressWarnings("serial")
	private void construireUI()
	{
		JPanel panelPlugin = new JPanel();
		panelPlugin.setSize(new Dimension(50, 100));
		jp.add(panelPlugin, BorderLayout.CENTER);
		panelPlugin.setLayout(new BorderLayout(0, 0));

		JPanel panelTitres = new JPanel();
		panelPlugin.add(panelTitres, BorderLayout.NORTH);
		panelTitres.setLayout(new BorderLayout(0, 0));

		paneGeneral = new JPanel();
		JLabel labelChoix = new JLabel("Catégorie :");
		panelTitres.add(paneGeneral,BorderLayout.NORTH);
		comboGeneral = new JComboBox<Object>();
		comboGeneral.addItemListener(this);
		comboGeneral.setActionCommand("chargerUnitesSource");
		paneGeneral.add(labelChoix);
		paneGeneral.add(comboGeneral);


		JSplitPane splitUnites = new JSplitPane();
		panelTitres.add(splitUnites,BorderLayout.SOUTH);

		comboSource = new JComboBox<Object>();
		splitUnites.setLeftComponent(comboSource);
		comboSource.setActionCommand("chargerUnitesDestination");
		comboSource.addItemListener(this);

		comboResult = new JComboBox<Object>();
		splitUnites.setRightComponent(comboResult);
		splitUnites.setDividerLocation(450);
		comboResult.addItemListener(this);

		JPanel panelListes = new JPanel();
		panelPlugin.add(panelListes, BorderLayout.CENTER);
		panelListes.setLayout(new BorderLayout(0, 0));

		JSplitPane splitListes = new JSplitPane();
		panelListes.add(splitListes, BorderLayout.CENTER);

		tableSource = new JTable();
		tableSource.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		tableSource.setModel(new DefaultTableModel(
				new Object[][] {
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
				},
				new String[] {
						"Sélectionner les valeurs à convertir :"
				}
				) {
			@SuppressWarnings("rawtypes")
			Class[] columnTypes = new Class[] {
				Float.class
			};
			public Class<?> getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		JScrollPane jspSource = new JScrollPane(tableSource);
		splitListes.setLeftComponent(jspSource);
		tableSource.getModel().addTableModelListener(this);



		tableDestination = new JTable();
		tableDestination.setEnabled(false);
		tableDestination.setModel(new DefaultTableModel(
				new Object[][] {
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
				},
				new String[] {
						"Résultat :"
				}
				) {
			@SuppressWarnings("rawtypes")
			Class[] columnTypes = new Class[] {
				Float.class
			};
			public Class<?> getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		JScrollPane jspDestination = new JScrollPane(tableDestination);
		splitListes.setRightComponent(jspDestination);
		splitListes.setDividerLocation(450);

		/*convertisseur = new Convertisseur();
		vue = new VueConvertisseur();
		Controller controller = new Controller(convertisseur,vue,tableDestination,this);*/
		JPanel panelSouth;
		panelSouth = new JPanel();
		panelPlugin.add(panelSouth, BorderLayout.SOUTH);
		panelSouth.setLayout(new BorderLayout(0, 0));

		btnConvertir = new JButton("Convertir");
		btnConvertir.setEnabled(false);
		btnConvertir.setActionCommand("convertir");
		panelSouth.add(btnConvertir, BorderLayout.SOUTH);


		btnIntervertir = new JButton("Intervertir");
		btnIntervertir.setEnabled(false);
		btnIntervertir.setActionCommand("intervertir");
		btnIntervertir.addActionListener(this);
		panelSouth.add(btnIntervertir, BorderLayout.NORTH);
		tm=tableSource.getModel();
		jp.repaint();
		btnConvertir.addActionListener(this);
		//convertisseur.addObserver(controller);
	}
	public TableModel getModel(){
		return tm;
	}

	@Override
	public void itemStateChanged(ItemEvent arg0) {
		JComboBox<?> jb = (JComboBox<?>)(arg0.getSource());
		try{
			if(jb.getActionCommand().equals("chargerUnitesSource")){
				comboResult.setModel(new DefaultComboBoxModel<Object>());
				listeSource = listeGenerale.get(jb.getSelectedItem().toString());
				comboSource.setModel(new DefaultComboBoxModel<Object>(convertirSetToStrings(listeSource.keySet())));
				comboSource.setSelectedIndex(-1);
				comboSource.setSelectedIndex(0);
			}
			if(jb.getActionCommand().equals("chargerUnitesDestination")){

				listeDestination = listeSource.get(jb.getSelectedItem().toString());
				comboResult.setModel(new DefaultComboBoxModel<Object>(convertirSetToStrings(listeDestination.keySet())));
				if(!btnConvertir.isEnabled()){
					btnConvertir.setEnabled(true);
					btnIntervertir.setEnabled(true);
				}


			}
		}
		catch(NullPointerException e){}

	}
	@Override
	public void tableChanged(TableModelEvent arg0) {

	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		int[] vals;
		int i;
		DefaultTableModel aConvertir;
		if(arg0.getActionCommand().equals("convertir")){
			viderTable(tableDestination);
			aConvertir = new DefaultTableModel();
			aConvertir.setColumnCount(tableSource.getColumnCount());
			aConvertir.setRowCount(tableSource.getRowCount());
			ControleurConvertisseur cc = (ControleurConvertisseur)controleur;
			vals=tableSource.getSelectedRows();
			for(i=0;i<vals.length;i++){
				aConvertir.setValueAt(tableSource.getValueAt(vals[i], 0), vals[i], 0);
			}
			cc.configurerModeleInterne(comboGeneral.getSelectedItem().toString(),comboSource.getSelectedItem().toString(), comboResult.getSelectedItem().toString());
			cc.chargerModeleInterne("convertir",aConvertir);	
		}
		if(arg0.getActionCommand().equals("intervertir")){
			//sert a intervertir les valeurs.. A voir si utile ou pas
			/*TableModel temp = tableDestination.getModel();	
			tableDestination.setModel(tableSource.getModel());
			tableSource.setModel(temp);
			//tableSource.repaint();
			//tableDestination.repaint();*/	
			String newSource = comboResult.getSelectedItem().toString();
			String  newResult= comboSource.getSelectedItem().toString();
			comboSource.setSelectedIndex(getIndexByName(comboSource,newSource));
			comboResult.setSelectedIndex(getIndexByName(comboResult,newResult));
		}

	}
	private void viderTable(JTable jt){
		int i,j;
		for(j=0;j<jt.getColumnCount();j++){
			for(i=0;i>jt.getRowCount();i++){
				jt.setValueAt(null, i, j);
			}
		}

	}
	private Integer getIndexByName(JComboBox<Object> jcb,String search){
		int i;
		Integer result = null;
		for(i=0;i<jcb.getItemCount();i++){
			if(jcb.getItemAt(i).toString().equals(search)){
				result= new Integer(i);
			}
		}
		return result;
	}
	@Override
	public void installerUI(JPanel jp) {
		this.jp=jp;
		construireUI();
	}
	public void chargerCombos(HashMap<String,HashMap<String,HashMap<String,Float>>> listeGenerale){
		this.listeGenerale=listeGenerale;
		Set<String> listeString = listeGenerale.keySet();
		comboGeneral.setModel(new DefaultComboBoxModel<Object>(convertirSetToStrings(listeString)));
		comboGeneral.setSelectedIndex(-1);
		comboGeneral.setSelectedItem("Choisir");
	}
	private String[] convertirSetToStrings(Set<String> set){
		int max = set.size();
		String[] StringModele = new String[max];
		Object[] listeO = set.toArray();
		int i;
		for(i=0;i<max;i++){
			StringModele[i]= ((String)listeO[i]);
		}
		return StringModele;	
	}

}
